<?php

require_once('conf/DBConnection.php');

/**
 * Plugin para manejo de Noticias
 *
 */
class Transporte extends Entity {

	var $id;
	
	public function __construct() {
		parent::__construct();			
	}
	
	function getTransportes($provincia, $ciudad, $tipocamion) {
		$condition1 = "";
		$condition2 = "";
		if (!empty($ciudad))
			$condition0 = " AND C.id= '$ciudad'";
		if (!empty($provincia))
			$condition1 = " AND P.id= '$provincia'";
		if (!empty($tipocamion) && $tipocamion == 'chasisyacoplado'  )
			$condition2 = " AND chasisyacoplado = 1";
		else if (!empty($tipocamion) && $tipocamion == 'jaula'  )
			$condition2 = " AND jaula = 1";
		else if (!empty($tipocamion) && $tipocamion == 'jauladoble'  )
			$condition2 = " AND jauladoble = 1";
		DBConnection::getInstance();
		$query = "SELECT *, T.id as idTransporte"
				." FROM transportes as T INNER JOIN ciudades as C ON C.id=T.idCiudad INNER JOIN provincias as P ON P.id=T.idProvincia "
				." WHERE T.borrado = 0 $condition0 $condition1 $condition2 "
				." ORDER BY T.destacado DESC ";
		$result = mysql_query($query);	
		//echo $query;
		return $this->fetch($result);
	}

	function getTransportesDestacados() {
		DBConnection::getInstance();
		$query = "SELECT *"
				." FROM transportes AS T INNER JOIN ciudades as C ON C.id=T.idCiudad INNER JOIN provincias as P ON P.id=T.idProvincia "
				." WHERE T.borrado = 0 and destacado = 1"
						." ORDER BY nombre DESC";
		$result = mysql_query($query);
		//echo $query;
		return $this->fetch($result);
	}
	
	function getTransporte($id) {
		DBConnection::getInstance();
		$query = "SELECT *"
				." FROM transportes AS T INNER JOIN ciudades as C ON C.id=T.idCiudad INNER JOIN provincias as P ON P.id=T.idProvincia "
				." WHERE T.id = $id";
		$result = mysql_query($query);
		//echo $query;
		$r = $this->fetch($result);
		return $r[0]; 
	}
	
	function getCiudades(){
		DBConnection::getInstance();
		$query = "SELECT *, C.id as idCiudad  FROM ciudades as C INNER JOIN provincias as P ON C.idProvincia=P.id ORDER BY C.ciudad ";
		//echo $query;
		$result = mysql_query($query);
		return $this->fetch($result);
	}
	  
	function getCiudadesByProvincia($provincia){
		DBConnection::getInstance();
		$query = "SELECT *, C.id as idCiudad  FROM ciudades as C INNER JOIN provincias as P ON C.idProvincia=P.id where P.id=$provincia ORDER BY C.ciudad ";
		//echo $query;
		$result = mysql_query($query);
		return $this->fetch($result);
	}
	
	function getProvincias(){
		DBConnection::getInstance();
		$query = "SELECT *  FROM provincias ORDER BY provincia  ";
		//echo $query;
		$result = mysql_query($query);
		return $this->fetch($result);
	}
}

